package com.zjp.dataFormat.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zjp.dataFormat.pojo.parse.DataMonthVo;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.BiFunction;

public class JsonService {


    // 金额
    public static final String dataNum = "{\"202001\": 6, \"202002\": 3, \"202003\": 7, \"202004\": 5, \"202005\": 5, \"202006\": 8, \"202007\": 8, \"202008\": 7, \"202009\": 12, \"202010\": 10, \"202011\": 9, \"202012\": 11, \"202101\": 6, \"202102\": 5, \"202103\": 7, \"202104\": 5, \"202105\": 14, \"202106\": 7, \"202107\": 13, \"202108\": 8, \"202109\": 14, \"202110\": 7, \"202111\": 14, \"202112\": 6, \"202201\": 5, \"202202\": 5, \"202203\": 50, \"202204\": 2, \"202205\": 36, \"202206\": 26, \"202207\": 5, \"202208\": 37, \"202209\": 51, \"202210\": 18, \"202211\": 28, \"202212\": 28, \"202301\": 9, \"202302\": 26, \"202303\": 22, \"202304\": 24, \"202305\": 12}";

    public static final String dataNum2 = "{\"2020-01\": 6, \"2020-02\": 3, \"2020-03\": 7, \"2020-04\": 5, \"2020-05\": 5, \"2020-06\": 8, \"2020-07\": 8, \"2020-08\": 7, \"2020-09\": 12, \"2020-10\": 10, \"2020-11\": 9, \"2020-12\": 11, \"2021-01\": 6, \"2021-02\": 5, \"2021-03\": 7, \"2021-04\": 5, \"2021-05\": 14, \"2021-06\": 7, \"2021-07\": 13, \"2021-08\": 8, \"2021-09\": 14, \"2021-10\": 7, \"2021-11\": 14, \"2021-12\": 6, \"2022-01\": 5, \"2022-02\": 5, \"2022-03\": 50, \"2022-04\": 2, \"2022-05\": 36, \"2022-06\": 26, \"2022-07\": 5, \"2022-08\": 37, \"2022-09\": 51, \"2022-10\": 18, \"2022-11\": 28, \"2022-12\": 28, \"2023-01\": 9, \"2023-02\": 26, \"2023-03\": 22, \"2023-04\": 24, \"2023-05\": 12}";


    public static final String dataAmount = "{\"202001\": 477146.91, \"202002\": 159112.97, \"202003\": 486716.9, \"202004\": 358753.3, \"202005\": 497711.5, \"202006\": 670776.97, \"202007\": 630348.17, \"202008\": 600361.06, \"202009\": 825695.58, \"202010\": 732202.21, \"202011\": 891674.66, \"202012\": 854521.93, \"202101\": 462564.53, \"202102\": 339093.74, \"202103\": 216925.31, \"202104\": 369855.77, \"202105\": 831867.53, \"202106\": 509233.43, \"202107\": 878560.37, \"202108\": 670051.37, \"202109\": 741957.79, \"202110\": 436746.83, \"202111\": 1053240.73, \"202112\": 155629.11, \"202201\": 513989.54, \"202202\": 162623.06, \"202203\": 969754.92, \"202204\": 117363.28, \"202205\": 633575.33, \"202206\": 766871.85, \"202207\": 138267.26, \"202208\": 1172301.12, \"202209\": 821200.51, \"202210\": 566493.63, \"202211\": 897837, \"202212\": 322977.13, \"202301\": 542115.01, \"202302\": 343828.95, \"202303\": 557662.85, \"202304\": 408938.33, \"202305\": 556341.07}";


    // 解析


    public static void main(String[] args) {


        TreeMap<String, Object> strToMap = strToMap(dataNum2);

        List<DataMonthVo<Integer>> toList = toList(strToMap, (String key, Object obj) -> new DataMonthVo<>(key, Convert.toInt(obj)));

        List<DataMonthVo<BigDecimal>> list2 = toList(strToMap(dataAmount), (String key, Object obj) -> new DataMonthVo<>(key, Convert.toBigDecimal(obj)));

        System.out.println("-----------------");

    }


    public static TreeMap<String, Object> strToMap(String str) {
        JSONObject object = JSONUtil.parseObj(StrUtil.removeAll(str, StrUtil.DASHED));
        Map<String, Object> map = BeanUtil.beanToMap(object);
        return MapUtil.newTreeMap(map, Comparator.reverseOrder());
    }

    public static <R> List<DataMonthVo<R>> toList(TreeMap<String, Object> map, BiFunction<String, Object, DataMonthVo<R>> function) {
        List<DataMonthVo<R>> list = new ArrayList<>();
        supplement(map);
        Set<Map.Entry<String, Object>> entrySet = map.entrySet();
        for (Map.Entry<String, Object> next : entrySet) {
            list.add(function.apply(next.getKey(), next.getValue()));
        }
        return list;
    }


    public static void supplement(TreeMap<String, Object> map) {
        Set<String> key = getKey(map);
        if (CollUtil.isNotEmpty(key)) {
            for (String s : key) {
                if (!map.containsKey(s)) {
                    map.put(s, 0);
                }
            }
        }
    }


    /**
     * 只能处理以下格式
     * 202202
     *
     * @param map
     */
    public static Set<String> getKey(TreeMap<String, Object> map) {
        String firstKey = map.firstKey();
        String lastKey = map.lastKey();

        int size = map.keySet().size();

        String s1 = StrUtil.subPre(firstKey, 4) + "-" + StrUtil.subSuf(firstKey, 4) + "-" + "01";
        String s2 = StrUtil.subPre(lastKey, 4) + "-" + StrUtil.subSuf(lastKey, 4) + "-" + "01";
        DateTime startTime = DateUtil.parseDate(s1);

        int betweenMonth = (int) DateUtil.betweenMonth(startTime, DateUtil.parseDate(s2), true);
        int between = betweenMonth + 1 - size;

        if (between == 0) {
            return Collections.emptySet();
        }

        Set<String> strSet = new HashSet<>();
        for (int i = 1; i <= betweenMonth; i++) {
            int year = startTime.getField(DateField.YEAR);
            int month = startTime.getField(DateField.MONTH) + 1;
            String format = String.format("%d%02d", year, month);
            strSet.add(format);
            startTime = DateUtil.offsetMonth(startTime, -1);
        }
        return strSet;
    }
}
